package com.yunyi.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileStreamUtils {
    private FileStreamUtils() {
    }

    public static FileInputStream convertMultipartFileToInputStream(MultipartFile multipartFile) throws IOException {
        // 创建临时文件
        File tempFile = File.createTempFile("temp", null);

        // 将MultipartFile的数据写入临时文件
        try (FileOutputStream fos = new FileOutputStream(tempFile)) {
            fos.write(multipartFile.getBytes());
        }

        // 将临时文件转换为FileInputStream
        FileInputStream fileInputStream = new FileInputStream(tempFile);

        // 删除临时文件
        tempFile.delete();

        return fileInputStream;
    }
}
